﻿Imports System.IO
Imports System.Runtime.InteropServices

Public Class FrmInfo
    Dim FileCount As Integer
    Dim Showfile As System.Object
    Dim SavedPaths As Boolean = True
    Dim ReplaceDone As Boolean = True
    Dim CurrentAccount As Integer = 0
    Dim SelectedAccount As Integer = 0
    Dim CurrentAddAccount As Integer = 0
    Dim FileNumber As Integer = FreeFile()
    Dim VideoPath As String = String.Empty
    Dim U2bviewsPath As String = String.Empty
    Dim AccCountPath As String = String.Empty
    Dim DescriptionPath As String = String.Empty
    Private Const EM_SETCUEBANNER As Integer = &H1501

    Private Sub SetCueText(ByVal control As Control, ByVal text As String)
        SendMessage(control.Handle, EM_SETCUEBANNER, 0, text)
    End Sub

    Private Sub FrmInfo_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        U2bviewsPath = Application.StartupPath & "\u2bviews.txt"
        AccCountPath = Application.StartupPath & "\accountscount.txt"
        CurrentAccount = System.IO.File.ReadAllText(AccCountPath)
        Do Until CurrentAddAccount = CurrentAccount
            CurrentAddAccount += 1
            ListAccounts.Items.Add("Account " & CurrentAddAccount)
        Loop
        SetCueText(TxtUsername, "Username")
        SetCueText(TxtPassword, "Password, press F11 to show/hide password")
        TxtUsername.Text = GetSettingItem(U2bviewsPath, "account")
        TxtPassword.Text = GetSettingItem(U2bviewsPath, "password")
    End Sub

    Private Sub BtnAdd_Click(sender As Object, e As EventArgs) Handles BtnAdd.Click
        CurrentAccount += 1
        ListAccounts.Items.Add("Account " & CurrentAccount)
        FileOpen(FileNumber, AccCountPath, OpenMode.Output)
        PrintLine(FileNumber, CurrentAccount)
        FileClose(FileNumber)
    End Sub

    Private Sub BtnExit_Click(sender As Object, e As EventArgs) Handles BtnExit.Click
        Me.Close()
    End Sub

    Private Sub TimerSave_Tick(sender As Object, e As EventArgs) Handles TimerSave.Tick
        VideoPath = Application.StartupPath & "\videos\account" & SelectedAccount & ".txt"
        If SavedPaths = False Then
            Try
                TxtTemp.Text = String.Empty
                For Each Item As Object In ListPaths.Items
                    TxtTemp.Text &= Item.ToString & vbNewLine
                Next
                Dim Number As Integer = 2
                Dim TempString As String = SelectedAccount & "1: "
                For Each StringC As Char In TxtTemp.Text
                    TempString &= StringC
                    If StringC = vbLf Then
                        TempString &= SelectedAccount & Number.ToString & ": "
                        Number += 1
                    End If
                Next
                TxtTemp.Text = TempString
                TxtTemp.Text = Mid(TxtTemp.Text, 1, TxtTemp.Text.Length - 4)
                Dim FileNumber As Integer = 13
                FileOpen(FileNumber, VideoPath, OpenMode.Output)
                For Each Item As Object In TxtTemp.Lines
                    PrintLine(FileNumber, Item.ToString)
                Next
                FileClose(FileNumber)
                SavedPaths = True
                ListPaths.ForeColor = Color.Black
            Catch ex As Exception
                ListPaths.ForeColor = Color.Red
            End Try
        End If
    End Sub

    Private Sub BtnClear_Click(sender As Object, e As EventArgs) Handles BtnClear.Click
        TxtDescription.Text = String.Empty
        ListPaths.Items.Clear()
        SavedPaths = False
    End Sub

    Private Sub BtnRemove_Click(sender As Object, e As EventArgs) Handles BtnRemove.Click
        If Not ListAccounts.Items.Count <= 0 Then
            ListAccounts.Items.RemoveAt(ListAccounts.Items.Count - 1)
            CurrentAccount -= 1
            FileOpen(FileNumber, AccCountPath, OpenMode.Output)
            PrintLine(FileNumber, CurrentAccount)
            FileClose(FileNumber)
        End If
    End Sub

    Private Sub TimerChecks_Tick(sender As Object, e As EventArgs) Handles TimerChecks.Tick
        If ListAccounts.Items.Count >= 1 Then
            If SelectedAccount < 1 Then
                BtnClear.Enabled = False
                BtnAddVideos.Enabled = False
                TxtDescription.ReadOnly = True
                TxtDescription.Text = String.Empty
                TxtDescription.BackColor = Color.White
            Else
                TxtDescription.ReadOnly = False
                BtnAddVideos.Enabled = True
                BtnClear.Enabled = True
            End If
        Else
            BtnClear.Enabled = False
            BtnAddVideos.Enabled = False
            TxtDescription.ReadOnly = True
            TxtDescription.Text = String.Empty
            TxtDescription.BackColor = Color.White
        End If
    End Sub

    Private Sub ListPaths_KeyUp(sender As Object, e As KeyEventArgs) Handles ListPaths.KeyUp
        VideoPath = Application.StartupPath & "\videos\account" & SelectedAccount & ".txt"
        If e.KeyCode = Keys.Delete Then
            ListPaths.Items.RemoveAt(ListPaths.SelectedIndex)
            SavedPaths = False
        End If
    End Sub

    Public Function GetSettingItem(ByVal File As String, ByVal Identifier As String) As String
        Dim S As New IO.StreamReader(File) : Dim Result As String = ""
        Do While (S.Peek <> -1)
            Dim Line As String = S.ReadLine
            If Line.ToLower.StartsWith(Identifier.ToLower & ":") Then
                Result = Line.Substring(Identifier.Length + 2)
            End If
        Loop
        S.Dispose()
        Return Result
    End Function

    Private Sub TimerReplace_Tick(sender As Object, e As EventArgs) Handles TimerReplace.Tick
        If ReplaceDone = False Then
            For i As Integer = 0 To ListPaths.Items.Count - 1
                ListPaths.Items(i) = Mid(ListPaths.Items(i), 5)
                If Mid(ListPaths.Items(i).ToString, 1, 1) = " " Then
                    ListPaths.Items(i) = Mid(ListPaths.Items(i), 2)
                End If
                If Mid(ListPaths.Items(i).ToString, 1, 1) = " " Then
                    ListPaths.Items(i) = Mid(ListPaths.Items(i), 2)
                End If
                If Mid(ListPaths.Items(i).ToString, 1, 1) = " " Then
                    ListPaths.Items(i) = Mid(ListPaths.Items(i), 2)
                End If
            Next
            If ListPaths.Items(0).ToString = String.Empty Then
                ListPaths.Items.RemoveAt(0)
            End If
            ReplaceDone = True
        End If
    End Sub

    Private Sub TxtPassword_KeyUp(sender As Object, e As KeyEventArgs) Handles TxtPassword.KeyUp
        U2bviewsPath = Application.StartupPath & "\u2bviews.txt"
        If e.KeyCode = Keys.F11 Then
            If TxtPassword.PasswordChar = "*" Then
                TxtPassword.PasswordChar = ""
            Else
                TxtPassword.PasswordChar = "*"
            End If
        Else
            Try
                Dim FullPath As String = U2bviewsPath
                Dim Lines As Integer
                Dim aryText(1) As String
                aryText(0) = "account: " & TxtUsername.Text
                aryText(1) = "password: " & TxtPassword.Text
                Dim objWriter As New System.IO.StreamWriter(FullPath, False)
                For Lines = 0 To 1
                    objWriter.WriteLine(aryText(Lines))
                Next
                objWriter.Close()
                TxtUsername.ForeColor = Color.Black
                TxtPassword.ForeColor = Color.Black
            Catch ex As Exception
                TxtUsername.ForeColor = Color.Red
                TxtPassword.ForeColor = Color.Red
            End Try
        End If
    End Sub

    Private Sub TxtUsername_KeyUp(sender As Object, e As KeyEventArgs) Handles TxtUsername.KeyUp
        U2bviewsPath = Application.StartupPath & "\u2bviews.txt"
        Try
            Dim FullPath As String = U2bviewsPath
            Dim Lines As Integer
            Dim aryText(1) As String
            aryText(0) = "account: " & TxtUsername.Text
            aryText(1) = "password: " & TxtPassword.Text
            Dim objWriter As New System.IO.StreamWriter(FullPath, False)
            For Lines = 0 To 1
                objWriter.WriteLine(aryText(Lines))
            Next
            objWriter.Close()
            TxtUsername.ForeColor = Color.Black
            TxtPassword.ForeColor = Color.Black
        Catch ex As Exception
            TxtUsername.ForeColor = Color.Red
            TxtPassword.ForeColor = Color.Red
        End Try
    End Sub

    Private Sub BtnAddVideos_Click(sender As Object, e As EventArgs) Handles BtnAddVideos.Click
        Dim VideosDialog As New OpenFileDialog
        VideosDialog.Title = "Select videos"
        VideosDialog.Multiselect = True
        VideosDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
        VideosDialog.Filter = "Video file|*.avi; *.mp4; *.mkv"
        If VideosDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            For Each FileName As String In VideosDialog.FileNames
                ListPaths.Items.Add(FileName)
            Next
            SavedPaths = False
        End If
    End Sub

    Private Sub FrmInfo_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        FrmMain.Show()
    End Sub

    Private Sub TxtDescription_KeyUp(sender As Object, e As EventArgs) Handles TxtDescription.KeyUp
        Try
            DescriptionPath = Application.StartupPath & "\descriptions\account" & SelectedAccount & ".txt"
            Dim MyWriter As StreamWriter
            Dim MyStream As FileStream
            MyStream = New FileStream((DescriptionPath), FileMode.Create)
            MyWriter = New StreamWriter(MyStream)
            Dim MyItem As Object
            For Each MyItem In TxtDescription.Lines
                MyWriter.Write(MyItem.ToString & Environment.NewLine)
            Next
            MyWriter.Close()
            MyStream.Close()
            TxtDescription.ForeColor = Color.Black
        Catch ex As Exception
            TxtDescription.ForeColor = Color.Red
        End Try
    End Sub

    Private Sub ListAccounts_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListAccounts.SelectedIndexChanged
        ListPaths.Items.Clear()
        TxtDescription.Text = String.Empty
        SelectedAccount = ListAccounts.SelectedIndex + 1
        LabelSelectedAccount.Text = "Selected Account: " & ListAccounts.SelectedIndex + 1
        VideoPath = Application.StartupPath & "\videos\account" & SelectedAccount & ".txt"
        DescriptionPath = Application.StartupPath & "\descriptions\account" & SelectedAccount & ".txt"
        If My.Computer.FileSystem.FileExists(VideoPath) = True Then
            Dim SaveString1 As String = My.Computer.FileSystem.ReadAllText(VideoPath)
            Dim SaveString2 As String() = SaveString1.Split(vbNewLine)
            ListPaths.Items.AddRange(SaveString2)
            ReplaceDone = False
        End If
        If My.Computer.FileSystem.FileExists(DescriptionPath) = True Then
            TxtDescription.LoadFile(DescriptionPath, RichTextBoxStreamType.PlainText)
        End If
    End Sub

    <DllImport("user32.dll", CharSet:=CharSet.Auto)> Private Shared Function SendMessage(ByVal _
    hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, <MarshalAs(UnmanagedType.LPWStr)> ByVal lParam As String) As Int32
    End Function
End Class